package com.sync;

/**
 * 类锁的.class方法 实现
 * 在加对象的时候存在线程同时执行的情况，再次我们使用.calss对象来锁定同步代码块，这样保证多个线程顺序执行
 *
 * */
public class SyncObjectClass4 implements Runnable {
    static SyncObjectClass4 sync1 = new SyncObjectClass4();
    static SyncObjectClass4 sync2 = new SyncObjectClass4();
    @Override
    public void run() {
        method();
    }

    public void method(){
        synchronized (SyncObjectClass4.class){
            System.out.println("我是类所的静态方法形式,我叫"+Thread.currentThread().getName());
            try {
                Thread.sleep(3000);
            } catch (InterruptedException e) {

            }
            System.out.println("名称叫"+Thread.currentThread().getName()+ " 运行结束");
        }
    }


    public static void main(String[] args) {
        Thread t1 = new Thread(sync1);
        Thread t2 = new Thread(sync2);
        t1.start();
        t2.start();
        //通过isAlive方法检测是否线程还在继续存活
        while(t1.isAlive() || t2.isAlive());

        System.out.println("finished");

    }
}
